//
//  UIFont+Extension.swift
//  NetWork
//
//  Created by Delin Meng 孟德林 on 2018/9/21.
//  Copyright © 2018年 Delin Meng 孟德林. All rights reserved.
//

import UIKit

/// Attribute String
struct WLAttributedString {

    enum Font:String {
        case light = "PingFangSC-Light"
        case medium = "PingFangSC-Medium"
        case regular = "PingFangSC-Regular"
    }

    enum Line{
        case none
        case topLine
        case midLine
        case underLine
    }

    static func attributeString(content:String,
                                font:WLAttributedString.Font,
                                size:CGFloat,
                                alignment:NSTextAlignment? = NSTextAlignment.center,
                                color:UIColor?,
                                line:Line = .none,lineSpacing:CGFloat = 0) -> NSMutableAttributedString
    {
        let attStr = NSMutableAttributedString(string: content)

        //set color
        if let c = color {
            attStr.addAttribute(NSAttributedStringKey.foregroundColor, value: c, range: NSRange(location: 0, length: content.count))
        }

        let style = NSMutableParagraphStyle()
        if let align = alignment {
            style.alignment = align
        } else {
            style.alignment = NSTextAlignment.center
        }

        attStr.addAttribute(NSAttributedStringKey.paragraphStyle, value: style, range: NSRange(location: 0, length: content.count))


        //set font
        switch font {
        case .light:
            var font = UIFont(name: WLAttributedString.Font.light.rawValue, size: size)
            if font == nil {
                font = UIFont.systemFont(ofSize: size)
            }
            attStr.addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: content.count))
            break
        case .medium:
            var font = UIFont(name: WLAttributedString.Font.medium.rawValue, size: size)
            if font == nil {
                font = UIFont.systemFont(ofSize: size)
            }
            attStr.addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: content.count))
            break
        case .regular:
            var font = UIFont(name: WLAttributedString.Font.regular.rawValue, size: size)
            if font == nil {
                font = UIFont.systemFont(ofSize: size)
            }
            attStr.addAttribute(NSAttributedStringKey.font, value: font, range: NSRange(location: 0, length: content.count))
            break
        }

        switch line{
        case .none:
            break
        case .topLine:
            break
        case .midLine:
            attStr.addAttribute(NSAttributedStringKey.strikethroughStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attStr.length))
            break
        case .underLine:
            attStr.addAttribute(NSAttributedStringKey.underlineStyle, value: NSUnderlineStyle.styleSingle.rawValue, range: NSMakeRange(0, attStr.length))
            break

        }

        if lineSpacing > 0{
            let paragraphStyle = NSMutableParagraphStyle()
            paragraphStyle.lineSpacing = lineSpacing

            // Line spacing attribute
            attStr.addAttribute(NSAttributedStringKey.paragraphStyle, value:paragraphStyle, range:NSMakeRange(0, attStr.length))
        }

        return attStr
    }
}

extension UIFont{
    static func PingFangSC(font:WLAttributedString.Font,ofSize:CGFloat) -> UIFont{
        if #available(iOS 9.0, *) {
            var pingfang = UIFont(name: font.rawValue, size: ofSize)
            if pingfang == nil{
                pingfang = UIFont.systemFont(ofSize: ofSize)
            }
            return pingfang!
        } else {
            return UIFont.systemFont(ofSize: ofSize)
        }
    }
}
